package org.jetnuke.modules.reviews;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.ReviewsDAO;
import org.jetnuke.entities.Reviews;
import org.jetnuke.entities.ReviewsComments;

public class ShowReview implements IAction {
	int id;

	public void setId(int id) {
		this.id = id;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		List comments=DataAccessDriver.getInst().newReviewsCommentDAO().selectByRID(id);
		Iterator it =comments.iterator();
		HashMap userMap=new HashMap();
		vc.put("userMap",userMap);
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		while(it.hasNext()){
			ReviewsComments rc=(ReviewsComments)it.next();
			int userid=rc.getUSERID();
			Integer uS=new Integer(userid);
			if(!userMap.containsKey(uS)){
				User user=um.selectById(userid);
				userMap.put(uS,user);
			}

		}
		vc.put("comments",comments);
		ReviewsDAO rd=DataAccessDriver.getInst().newReviewsDAO();
		Reviews rv=rd.retrieveByPK(id);
		rd.incHits(rv);
		vc.put("review", rv);
		return new ActionForward(op, info);
	}

}
